
<html lang=""><head>
    <title>开放接口 · MINA</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta name="description" content="">
    <meta name="generator" content="GitBook 3.2.0">




    <link rel="stylesheet" href="../../gitbook/style.css">




    <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">



    <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">























    <meta name="HandheldFriendly" content="true">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">


    <link rel="next" href="signature.html">


    <link rel="prev" href="ui-other.html">


</head>
<body>

<div class="book with-summary">
    <div>

        <div class="sub_nav_box">
            <div class="sub_nav_inner">




                <ul class="top_sub_nav">




                    <li class="sub_nav_item " data-level="1.1" data-path="../">

                        <a href="../">


                            简易教程

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.2" data-path="../framework/MINA.html">

                        <a href="../framework/MINA.html">


                            框架

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.3" data-path="../component/">

                        <a href="../component/">


                            组件

                        </a>

                    </li>

                    <li class="sub_nav_item selected" data-level="1.4" data-path="./">

                        <a href="./">


                            API

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.5" data-path="../devtools/devtools.html">

                        <a href="../devtools/devtools.html">


                            工具

                        </a>

                    </li>

                    <li class="sub_nav_item " data-level="1.6" data-path="../qa/qa.html">

                        <a href="../qa/qa.html">


                            Q&amp;A

                        </a>

                    </li>




                </ul>




                <!-- <h1>开放接口</h1> -->
                <div id="book-search-input" role="search">
                    <form>
                        <label for="search-input" class="search-icon" id="js-search-icon"></label>
                        <input type="text" id="search-input" name="search-input" placeholder="搜索">
                        <button type="reset" class="clear-input-btn"></button>
                    </form>
                </div>
            </div>
        </div>

        <div class="book-summary">


            <nav role="navigation">





                <ul class="summary">


                    <li class="chapter  open" data-level="1.4.1" data-path="api-network.html">

                        <a href="api-network.html">


                            网络

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.4.1.1" data-path="network-request.html">

                                <a href="network-request.html">


                                    发起请求

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.1.2" data-path="network-file.html">

                                <a href="network-file.html">


                                    上传、下载

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.1.2.1" data-path="network-file.html">

                                        <a href="network-file.html#wxuploadfileobject">


                                            wx.uploadFile

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.1.2.2" data-path="network-file.html">

                                        <a href="network-file.html#wxdownloadfileobject">


                                            wx.downloadFile

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.4.1.3" data-path="network-socket.html">

                                <a href="network-socket.html">


                                    WebSocket

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.1.3.1" data-path="network-socket.html">

                                        <a href="network-socket.html#wxconnectsocketobject">


                                            wx.connectSocket

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.1.3.2" data-path="network-socket.html">

                                        <a href="network-socket.html#wxonsocketopencallback">


                                            wx.onSocketOpen

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.1.3.3" data-path="network-socket.html">

                                        <a href="network-socket.html#wxonsocketerrorcallback">


                                            wx.onSocketError

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.1.3.4" data-path="network-socket.html">

                                        <a href="network-socket.html#wxsendsocketmessageobject">


                                            wx.sendSocketMessage

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.1.3.5" data-path="network-socket.html">

                                        <a href="network-socket.html#wxonsocketmessagecallback">


                                            wx.onSocketMessage

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.1.3.6" data-path="network-socket.html">

                                        <a href="network-socket.html#wxclosesocket">


                                            wx.closeSocket

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.1.3.7" data-path="network-socket.html">

                                        <a href="network-socket.html#wxonsocketclosecallback">


                                            wx.onSocketClose

                                        </a>



                                    </li>


                                </ul>

                            </li>


                        </ul>

                    </li>

                    <li class="chapter  open" data-level="1.4.2" data-path="media-picture.html">

                        <a href="media-picture.html">


                            媒体

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.4.2.1" data-path="media-picture.html">

                                <a href="media-picture.html">


                                    图片

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.2.1.1" data-path="media-picture.html">

                                        <a href="media-picture.html#wxchooseimageobject">


                                            wx.chooseImage

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.1.2" data-path="media-picture.html">

                                        <a href="media-picture.html#wxpreviewimageobject">


                                            wx.previewImage

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.4.2.2" data-path="media-record.html">

                                <a href="media-record.html">


                                    录音

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.2.2.1" data-path="media-record.html">

                                        <a href="media-record.html#wxstartrecordobject">


                                            wx.startRecord

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.2.2" data-path="media-record.html">

                                        <a href="media-record.html#wxstoprecord">


                                            wx.stopRecord

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.4.2.3" data-path="media-voice.html">

                                <a href="media-voice.html">


                                    音频播放控制

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.2.3.1" data-path="media-voice.html">

                                        <a href="media-voice.html#wxplayvoiceobject">


                                            wx.playVoice

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.3.2" data-path="media-voice.html">

                                        <a href="media-voice.html#wxpausevoice">


                                            wx.pauseVoice

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.3.3" data-path="media-voice.html">

                                        <a href="media-voice.html#wxstopvoice">


                                            wx.stopVoice

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.4.2.4" data-path="media-background-audio.html">

                                <a href="media-background-audio.html">


                                    音乐播放控制

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.2.4.1" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxgetbackgroundaudioplayerstateobject">


                                            wx.getBackgroundAudioPlayerState

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.4.2" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxplaybackgroundaudioobject">


                                            wx.playBackgroundAudio

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.4.3" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxpausebackgroundaudio">


                                            wx.pauseBackgroundAudio

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.4.4" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxseekbackgroundaudioobject">


                                            wx.seekBackgroundAudio

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.4.5" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxstopbackgroundaudio">


                                            wx.stopBackgroundAudio

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.4.6" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxonbackgroundaudioplaycallback">


                                            wx.onBackgroundAudioPlay

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.4.7" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxonbackgroundaudiopausecallback">


                                            wx.onBackgroundAudioPause

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.2.4.8" data-path="media-background-audio.html">

                                        <a href="media-background-audio.html#wxonbackgroundaudiostopcallback">


                                            wx.onBackgroundAudioStop

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.4.2.5" data-path="file.html">

                                <a href="file.html">


                                    文件

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.2.6" data-path="media-video.html">

                                <a href="media-video.html">


                                    视频

                                </a>



                            </li>


                        </ul>

                    </li>

                    <li class="chapter  open" data-level="1.4.3" data-path="data.html">

                        <a href="data.html">


                            数据

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.4.3.1" data-path="data.html">

                                <a href="data.html">


                                    数据缓存

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.3.1.1" data-path="data.html">

                                        <a href="data.html#wxsetstorageobject">


                                            wx.setStorage

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.3.1.2" data-path="data.html">

                                        <a href="data.html#wxsetstoragesynckeyvalue">


                                            wx.setStorageSync

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.3.1.3" data-path="data.html">

                                        <a href="data.html#wxgetstorageobject">


                                            wx.getStorage

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.3.1.4" data-path="data.html">

                                        <a href="data.html#wxgetstoragesynckey">


                                            wx.getStorageSync

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.3.1.5" data-path="data.html">

                                        <a href="data.html#wxclearstorage">


                                            wx.clearStorage

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.3.1.6" data-path="data.html">

                                        <a href="data.html#wxclearstoragesync">


                                            wx.clearStorageSync

                                        </a>



                                    </li>


                                </ul>

                            </li>


                        </ul>

                    </li>

                    <li class="chapter  open" data-level="1.4.4" data-path="location.html">

                        <a href="location.html">


                            位置

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.4.4.1" data-path="location.html">

                                <a href="location.html#wxgetlocationobject">


                                    获取位置

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.4.2" data-path="location.html">

                                <a href="location.html#wxopenlocationobject">


                                    查看位置

                                </a>



                            </li>


                        </ul>

                    </li>

                    <li class="chapter  open" data-level="1.4.5" data-path="device.html">

                        <a href="device.html">


                            设备

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.4.5.1" data-path="device.html">

                                <a href="device.html#wxgetnetworktypeobject">


                                    网络状态

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.5.2" data-path="device.html">

                                <a href="device.html#wxgetsysteminfoobject">


                                    系统信息

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.5.3" data-path="device.html">

                                <a href="device.html#wxonaccelerometerchangecallback">


                                    重力感应

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.5.4" data-path="device.html">

                                <a href="device.html#wxoncompasschangecallback">


                                    罗盘

                                </a>



                            </li>


                        </ul>

                    </li>

                    <li class="chapter  open" data-level="1.4.6" data-path="ui.html">

                        <a href="ui.html">


                            界面

                        </a>



                        <ul class="articles">


                            <li class="chapter  " data-level="1.4.6.1" data-path="ui.html">

                                <a href="ui.html">


                                    设置导航条

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.6.2" data-path="ui-navigate.html">

                                <a href="ui-navigate.html">


                                    导航

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.6.3" data-path="api-animation.html">

                                <a href="api-animation.html">


                                    动画

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.6.4" data-path="api-canvas.html">

                                <a href="api-canvas.html">


                                    绘图

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.6.5" data-path="ui-other.html">

                                <a href="ui-other.html">


                                    其他

                                </a>



                            </li>


                        </ul>

                    </li>

                    <li class="chapter active open" data-level="1.4.7" data-path="api-login.html">

                        <a href="api-login.html">


                            开放接口

                        </a>



                        <ul class="articles">


                            <li class="chapter active " data-level="1.4.7.1" data-path="api-login.html">

                                <a href="api-login.html">


                                    登录

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.7.1.1" data-path="signature.html">

                                        <a href="signature.html">


                                            签名加密

                                        </a>



                                    </li>


                                </ul>

                            </li>

                            <li class="chapter  " data-level="1.4.7.2" data-path="open.html">

                                <a href="open.html">


                                    用户信息

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.7.3" data-path="api-pay.html">

                                <a href="api-pay.html">


                                    微信支付

                                </a>



                            </li>

                            <li class="chapter  " data-level="1.4.7.4" data-path="notice.html">

                                <a href="notice.html">


                                    模板消息

                                </a>



                                <ul class="articles">


                                    <li class="chapter  " data-level="1.4.7.4.1" data-path="notice.html">

                                        <a href="notice.html#使用说明">


                                            使用说明

                                        </a>



                                    </li>

                                    <li class="chapter  " data-level="1.4.7.4.2" data-path="notice.html">

                                        <a href="notice.html#接口说明">


                                            接口说明

                                        </a>



                                    </li>


                                </ul>

                            </li>


                        </ul>

                    </li>


                </ul>


            </nav>


        </div>

        <div class="book-body">

            <div class="body-inner">

                <!--                     
                
                <div class="book-header" role="navigation">
                    
                </div>
                
                
                 -->

                <div class="page-wrapper" tabindex="-1" role="main">
                    <div class="page-inner">

                        <div id="book-search-results">
                            <div class="search-noresults">

                                <section class="normal markdown-section">

                                    <h2 id="wxloginobject">wx.login(OBJECT)</h2><p>​    调用接口获取<strong>登录凭证(code)</strong>进而换取用户登录态信息，包括用户的<strong>唯一标识(openid)</strong> 及本次登录的 <strong>会话密钥(session_key)</strong>。<strong>用户数据的加解密通讯</strong>需要依赖会话密钥完成。</p><p><strong>OBJECT参数说明：</strong></p><table>
                                    <thead>
                                    <tr><th>参数名</th>
                                        <th>类型</th>
                                        <th>必填</th>
                                        <th>说明</th>
                                    </tr></thead><tbody>
                                <tr><td>success</td>
                                    <td>Function</td>
                                    <td>否</td>
                                    <td>接口调用成功的回调函数</td>
                                </tr><tr><td>fail</td>
                                    <td>Function</td>
                                    <td>否</td>
                                    <td>接口调用失败的回调函数</td>
                                </tr><tr><td>complete</td>
                                    <td>Function</td>
                                    <td>否</td>
                                    <td>接口调用结束的回调函数（调用成功、失败都会执行）</td>
                                </tr></tbody></table>

                                    <p><strong>success返回参数说明：</strong></p><table>
                                    <thead>
                                    <tr><th>参数名</th>
                                        <th>类型</th>
                                        <th>说明</th>
                                    </tr></thead><tbody>
                                <tr><td>errMsg</td>
                                    <td>String</td>
                                    <td>调用结果</td>
                                </tr><tr><td>code</td>
                                    <td>String</td>
                                    <td>用户允许登录后，回调内容会带上code（有效期五分钟），开发者需要将code发送到开发者服务器后台，使用<code>code换取session_key</code> api，将code换成openid和session_key</td>
                                </tr></tbody></table>

                                    <p><strong>示例代码：</strong></p><pre><code class="lang-javascript"><span class="hljs-comment">//app.js</span>
App({
  onLaunch:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    wx.login({
      success:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">res</span>)</span>{
        <span class="hljs-keyword">if</span>(res.code){
          <span class="hljs-comment">//发起网络请求</span>
          wx.request({
            url:<span class="hljs-string">"https://test.com/onLogin"</span>,
            data:{
              code:res.code
            },
            success:<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">json</span>)</span>{
            }
          })
        }<span class="hljs-keyword">else</span>{
          <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"获取用户登录态失败！"</span> + res.errMsg);
        }
      }
    });  
  }
})</code></pre>
                                    <p><strong>code换取session_key</strong></p><p>​    这是一个HTTP接口，开发者服务器使用<strong>登录凭证code</strong>获取session_key和openid。其中session_key是对用户数据进行<a href="signature.html">加密签名</a>的密钥。为了自身应用安全，开发者<strong>不应该允许session_key在网络上传输</strong>。</p><pre><code>https://api.weixin.qq.com/sns/jscode2session?appid=APPID&amp;secret=SECRET&amp;js_code=JSCODE&amp;grant_type=authorization_code</code></pre>
                                    <p><strong>请求参数：</strong></p><table>
                                    <thead>
                                    <tr><th style="text-align:left">参数</th>
                                        <th>必填</th>
                                        <th>说明</th>
                                    </tr></thead><tbody>
                                <tr><td style="text-align:left">appid</td>
                                    <td>是</td>
                                    <td>公众号唯一标识</td>
                                </tr><tr><td style="text-align:left">secret</td>
                                    <td>是</td>
                                    <td>公众号的app secret</td>
                                </tr><tr><td style="text-align:left">js_code</td>
                                    <td>是</td>
                                    <td>登录时获取的code</td>
                                </tr><tr><td style="text-align:left">grant_type</td>
                                    <td>是</td>
                                    <td>填写为authorization_code</td>
                                </tr></tbody></table>

                                    <p><strong>返回参数：</strong></p><table>
                                    <thead>
                                    <tr><th style="text-align:left">参数</th>
                                        <th>说明</th>
                                    </tr></thead><tbody>
                                <tr><td style="text-align:left">openid</td>
                                    <td>用户唯一标识</td>
                                </tr><tr><td style="text-align:left">session_key</td>
                                    <td>会话密钥</td>
                                </tr><tr><td style="text-align:left">expires_in</td>
                                    <td>会话有效期, 以秒为单位, 例如2592000代表会话有效期为30天</td>
                                </tr></tbody></table>

                                    <p><strong>返回说明：</strong></p><pre><code class="lang-javascript"><span class="hljs-comment">//正常返回的JSON数据包</span>
{
      <span class="hljs-string">"openid"</span>:<span class="hljs-string">"OPENID"</span>,
      <span class="hljs-string">"session_key"</span>:<span class="hljs-string">"SESSIONKEY"</span>
      <span class="hljs-string">"expires_in"</span>:<span class="hljs-number">2592000</span>
}
<span class="hljs-comment">//错误时返回JSON数据包(示例为Code无效)</span>
{
    <span class="hljs-string">"errcode"</span>:<span class="hljs-number">40029</span>,
    <span class="hljs-string">"errmsg"</span>:<span class="hljs-string">"invalid code"</span>
}</code></pre>
                                    <h2 id=""></h2>

                                </section>

                            </div>
                            <div class="search-results">
                                <div class="has-results">

                                    <h1 class="search-results-title"><span class="search-results-count"></span> 个结果 "<span class="search-query"></span>"</h1>
                                    <ul class="search-results-list"></ul>

                                </div>
                                <div class="no-results">

                                    <h1 class="search-results-title">没有找到相关内容 "<span class="search-query"></span>"</h1>

                                </div>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="foot" id="footer">
                    <ul class="links ft">
                        <li class="links_item"><a href="">关于腾讯</a></li>
                        <li class="links_item"><a href="">服务协议</a></li>
                        <li class="links_item"><a href="">运营规范</a></li>
                        <li class="links_item"><a href="">辟谣中心</a></li>
                        <li class="links_item"><a href="">客服中心</a></li>
                        <li class="links_item"><a href="">联系邮箱</a></li>
                        <li class="links_item"><a href="">侵权投诉</a></li>
                        <li class="links_item">
                            <p class="copyright">Copyright © 2012-<span id="s_copyright_year"></span> Tencent. All Rights Reserved.</p>
                        </li>
                    </ul>
                </div>

            </div>



            <a href="ui-other.html" class="navigation navigation-prev " aria-label="Previous page: 其他">
                <i class="fa fa-angle-left"></i>
            </a>


            <a href="signature.html" class="navigation navigation-next " aria-label="Next page: 签名加密">
                <i class="fa fa-angle-right"></i>
            </a>



        </div>
    </div>


    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>


    <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>



    <script src="../../gitbook/gitbook-plugin-search/search.js"></script>



    <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>



    <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>


    




</div></body></html>